package com.example.project.controller;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.entity.Issue;
import com.example.project.entity.IssuePrioritie;
import com.example.project.entity.IssueStatus;
import com.example.project.entity.Project;
import com.example.project.entity.Tracker;
import com.example.project.entity.User;
import com.example.project.service.CommonService;
import com.example.project.service.IssuePrioritieService;
import com.example.project.service.IssueService;
import com.example.project.service.IssueStatusService;
import com.example.project.service.ProjectService;
import com.example.project.service.TrackerService;
import com.example.project.service.UserService;
import com.example.project.utils.DateUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

/**
 * 我的工作台
 */
@Controller
@RequestMapping("/my/workbench")
public class MyWorkbenchController extends BaseController {

	private String prefix = "workbench";

	@Autowired
	private UserService userService;
	
	@Autowired
	private CommonService commonService;

	@Autowired
	private IssueService issueService;

	@Autowired
	private ProjectService projectsService;

	@Autowired
	private IssueStatusService issueStatusService;
	
	@Autowired
	private IssuePrioritieService issuePrioritieService;

	@Autowired
	private TrackerService trackersService;

	/**
	 * 时间轴
	 */
	@RequiresPermissions("my:workbench:timeline")
	@GetMapping("/timeline")
	public String timeline(ModelMap mmap) {
		mmap.put("curUserId", getUserId());
		// 项目列表
		List<Project> projectList = projectsService.findAllList(1);
		mmap.put("projectList", projectList);
		
		//优先级
		List<IssuePrioritie> issuePrioritieList= issuePrioritieService.findAllList();
		mmap.put("issuePrioritieList", issuePrioritieList);

		// 跟踪列表
		List<Tracker> trackerList = trackersService.findAllList();
		mmap.put("trackerList", trackerList);
		// 状态列表
		List<IssueStatus> issueStatuseList = issueStatusService.findAllList();
		mmap.put("issueStatuseList", issueStatuseList);
		// 用户
		List<User> userList = userService.findAllList();
		mmap.put("userList", userList);
		
		mmap.put("curUserId", getUserId());

		mmap.put("sDate", DateUtils.getDateBefore(30));
		mmap.put("eDate", DateUtils.getDate());

		return prefix + "/timeline";
	}

	/**
	 * 获取问题统计图表数据
	 */
	@RequiresPermissions("my:workbench:timeline")
	@PostMapping("/timeline/view")
	public String timelineView(ModelMap mmap, Issue issue) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dataScope", "order by update_time desc, create_time desc");
		issue.setParams(params);
		// 如果有搜索条件，使用搜索条件过滤
		List<Issue> issueList = issueService.findListByCondition(issue);
		mmap.put("issueList", issueList);
		
		List<IssuePrioritie> issuePrioritieList = commonService.getIssuePrioritieList();
		mmap.put("issuePrioritieList", issuePrioritieList);
		return prefix + "/timeline_view";
	}

	/**
	 * 日程安排
	 */
	@RequiresPermissions("my:workbench:schedule")
	@GetMapping("/schedule")
	public String schedule(ModelMap mmap) {
		Issue issue = new Issue();
		LocalDate lastMonthFirstDate = DateUtils.getLastMonthFirstDate(LocalDate.now());
		issue.setsDate(DateUtils.formatLocalDate(lastMonthFirstDate, DateUtils.YYYY_MM_DD));
		issue.seteDate(DateUtils.getDate());
		try {
			List<Issue> issues = issueService.findListByCondition(issue);
			Map<String, List<Issue>> dataMap = issues.stream().collect(Collectors
					.groupingBy(info -> DateUtils.formatLocalDateTime(info.getCreateTime(), DateUtils.YYYY_MM_DD)));

			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.registerModule(new JavaTimeModule());
			objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
			String jsonData = objectMapper.writeValueAsString(dataMap);
			mmap.put("dataMap", jsonData);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return prefix + "/schedule";
	}

	/**
	 * 获取问题统计图表数据
	 */
	@RequiresPermissions("my:workbench:schedule")
	@GetMapping("/queryIssueList")
	@ResponseBody
	public AjaxResult queryIssueList(String checkDate) {
		String jsonData = "";
		try {
			LocalDate localDate = DateUtils.parseLocalDate(checkDate);
			Issue issue = new Issue();
			LocalDate lastMonthFirstDate = DateUtils.getLastMonthFirstDate(localDate);
			issue.setsDate(DateUtils.formatLocalDate(lastMonthFirstDate, DateUtils.YYYY_MM_DD));

			LocalDate lastMonthLastDate = DateUtils.getLastMonthLastDate(localDate);
			issue.seteDate(DateUtils.formatLocalDate(lastMonthLastDate, DateUtils.YYYY_MM_DD));
			List<Issue> issueList = issueService.findListByCondition(issue);

			Map<String, List<Issue>> dataMap = issueList.stream().collect(Collectors
					.groupingBy(info -> DateUtils.formatLocalDateTime(info.getCreateTime(), DateUtils.YYYY_MM_DD)));

			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.registerModule(new JavaTimeModule());
			objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
			jsonData = objectMapper.writeValueAsString(dataMap);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return AjaxResult.success("请求成功！！！", jsonData);
	}

}
